package com.imjohn.controller;

import com.github.pagehelper.PageInfo;
import com.imjohn.entity.Aticle;
import com.imjohn.entity.Top_mid;
import com.imjohn.entity.User;
import com.imjohn.service.AticleService;
import com.imjohn.service.PageService;
import com.imjohn.service.TopmidService;
import com.imjohn.service.UserService;
import com.imjohn.util.respUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangwei
 * @date 2021-07-21 下午 4:35
 */
@RestController
@RequestMapping("/api")
public class AticleController {
    @Autowired
    private AticleService aticleService;
    @Autowired
    private UserService userService;
    @Autowired
    private PageService pageService;
    @Autowired
    private TopmidService topmidService;

    /*写入文章*/
    @RequestMapping("/writeAticle")
    public Map WriteAticle(HttpServletRequest request, @RequestBody Aticle aticle) {
        HttpSession session = request.getSession();
        String jwtToekn = request.getHeader("X-Token");
        String token = (String) session.getAttribute("token");
        String userName = (String) session.getAttribute("userName");
        User user = userService.getUserRoler(userName);
        String rolerName = user.getRoler();
        if (!rolerName.equals("admin")) {
            String fReason = "非管理员无法发布文章!";
            return respUtil.respFailer(fReason); 
        }
        if (!jwtToekn.equals(token)) {
            String fReason = "请勿重复提交!";
            return respUtil.respFailer(fReason);
        }
        int i = aticleService.AddAticle(aticle);
        if (i == 1) {
            Map map = new HashMap();
            map.put("msg", "success");
            return respUtil.respSuccessHash(map);
        } else {
            String fReason = "发布失败!";
            return respUtil.respFailer(fReason);
        }
    }
    /*根据用户名查文章*/
    @RequestMapping("/getArticle")
    public Map getArticle(HttpServletRequest request, @RequestBody Map map) {
        String jwtToekn = request.getHeader("X-Token");
        HttpSession session = request.getSession();
        String token = (String) session.getAttribute("token");
        String userName = (String) session.getAttribute("userName");
        map.put("userName", userName);
        if (!jwtToekn.equals(token)) {
            String fReason = "请勿重复提交!";
            return respUtil.respFailer(fReason);
        }
        if (map.get("pageNum") != null && map.get("pageNum") != null) {
            PageInfo<Aticle> pageInfo = pageService.GetByNameAticle(Integer.parseInt(map.get("pageNum").toString()), Integer.parseInt(map.get("pageSize").toString()), userName);
            Map res = new HashMap();
            List<Aticle> iMap =  pageInfo.getList();
            if (iMap.size() > 0) {
                res.put("list" ,iMap);
                res.put("total" ,pageInfo.getTotal());
                return respUtil.respSuccessHash(res);
            } else {
                String fReason = "您没有文章哦!";
                return respUtil.respFailer(fReason);
            }
        } else {
            List<HashMap> iMap =  aticleService.GetAticle(userName);
            if(userName != null && iMap.size() > 0) {
                return respUtil.respSuccess(iMap);
            } else {
                String fReason = "您还没写文章哟!";
                return respUtil.respFailer(fReason);
            }
        }
    }

    /*根据文章id查文章 一对一 左连接*/
    @RequestMapping("/getArticleById")
    public Map getArticleById(HttpServletRequest request, @RequestBody Map map) {
        String jwtToekn = request.getHeader("X-Token");
        HttpSession session = request.getSession();
        String token = (String) session.getAttribute("token");
        if (!jwtToekn.equals(token)) {
            String fReason = "请勿重复提交!";
            return respUtil.respFailer(fReason);
        }
        int id = Integer.parseInt(map.get("id").toString());
        List<HashMap> iMap =  aticleService.getAticleById(id);
        if(iMap.size() > 0) {
            return respUtil.respSuccess(iMap);
        } else {
            String fReason = "系统错误!";
            return respUtil.respFailer(fReason);
        }

    }
    /*根据文章id查文章 结果嵌套*/
    @RequestMapping("/getArticleById2")
    public Map getArticleById2(HttpServletRequest request, @RequestBody Map map) {
        String jwtToekn = request.getHeader("X-Token");
        HttpSession session = request.getSession();
        String token = (String) session.getAttribute("token");
        if (!jwtToekn.equals(token)) {
            String fReason = "请勿重复提交!";
            return respUtil.respFailer(fReason);
        }
        int id = Integer.parseInt(map.get("id").toString());
        Map imap = new HashMap();
        Aticle aticle = aticleService.getAticleById2(id);
        if (aticle != null) {
            imap.put("data", aticle);
            imap.put("respCode" ,"00000000");
            return imap;
        } else {
            imap.put("data", "系统错误");
            imap.put("respCode" ,"00000000");
            return imap;
        }
    }

    /*根据文章类型查文章*/
    @RequestMapping("/getTypeArticle")
    public Map GetTypeAticle(@RequestBody Map map) {
        List<HashMap> iMap =  aticleService.GetTypeAticle(map.get("aticleType").toString());
        if(iMap.size() > 0) {
            return respUtil.respSuccess(iMap);
        } else {
            String fReason = "还没写文章哟!";
            return respUtil.respFailer(fReason);
        }
    }
    /*查询所有文章*/
    @RequestMapping("/getAllArticle")
    public Map getAllArticle() {
        Map res = new HashMap();
        List<HashMap> iMap = aticleService.GetAllAticle();
        if (iMap.size() > 0) {
            return respUtil.respSuccess(iMap);
        } else {
            String fReason = "还没写文章哟!";
            return respUtil.respFailer(fReason);
        }
    }
    /*文章修改*/
    @RequestMapping("/updateAticle")
    public Map updateAticle(HttpServletRequest request, @RequestBody Aticle aticle) {
        HttpSession session = request.getSession();
        String jwtToekn = request.getHeader("X-Token");
        String token = (String) session.getAttribute("token");
        String userName = (String) session.getAttribute("userName");
        User user = userService.getUserRoler(userName);
        String rolerName = user.getRoler();
        if (!rolerName.equals("admin")) {
            String fReason = "非管理员无法发布文章!";
            return respUtil.respFailer(fReason);
        }
        if (!jwtToekn.equals(token)) {
            String fReason = "请勿重复提交!";
            return respUtil.respFailer(fReason);
        }
        int i = aticleService.UpdateAticle(aticle);
        if (i == 1) {
            Map map = new HashMap();
            map.put("msg", "success");
            return respUtil.respSuccessHash(map);
        } else {
            String fReason = "发布失败!";
            return respUtil.respFailer(fReason);
        }
    }
    /*根据id修改*/
    @RequestMapping("/delAticle")
    public Map delAticle(HttpServletRequest request, @RequestBody Map map) {
        HttpSession session = request.getSession();
        String jwtToekn = request.getHeader("X-Token");
        String token = (String) session.getAttribute("token");
        String userName = (String) session.getAttribute("userName");
        User user = userService.getUserRoler(userName);
        String rolerName = user.getRoler();
        if (!rolerName.equals("admin")) {
            String fReason = "非管理员无法发布文章!";
            return respUtil.respFailer(fReason);
        }
        if (!jwtToekn.equals(token)) {
            String fReason = "请勿重复提交!";
            return respUtil.respFailer(fReason);
        }
        int i = aticleService.DelAticleById(Integer.parseInt(map.get("id").toString()));
        if (i == 1) {
            Map res = new HashMap();
            res.put("msg", "删除成功!");
            return respUtil.respSuccessHash(res);
        } else {
            String fReason = "删除失败!";
            return respUtil.respFailer(fReason);
        }
    }
    /*阅读量加1*/
    @RequestMapping("/addReadNum")
    public Map addReadNum(@RequestBody Map map){
        int i = aticleService.addReadNum(Integer.parseInt(map.get("id").toString()));
        if (i == 1) {
            Map res = new HashMap();
            res.put("msg", "成功!");
            return respUtil.respSuccessHash(res);
        } else {
            String fReason = "失败!";
            return respUtil.respFailer(fReason);
        }
    }
    /*点赞加1*/
    @RequestMapping("/addGoodNum")
    public Map addGoodNum(HttpServletRequest request, @RequestBody Top_mid top_mid){
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        top_mid.setUserId(userId);
        /**
         * 重复点赞校验
         * */
        List<HashMap> ress =  topmidService.isGoodFlag(userId, top_mid.getAticleId());
        if (ress != null && ress.size() == 1) {
            /**
             * 更新文章点赞数量
             * */
            int i = aticleService.subGoodNum(Integer.parseInt(top_mid.getAticleId()));
            int k = topmidService.cancelGoodFlag(top_mid);
            if (k == 1 && i == 1) {
                Map res = new HashMap();
                res.put("msg", "取消点赞");
                return respUtil.respSuccessHash(res);
            } else {
                String fReason = "系统错误!";
                return respUtil.respFailer(fReason);
            }
        }
        /**
         *
         * 未点过赞，插表
         * */
        int j = topmidService.setGoodFlag(userId, top_mid.getAticleId(), "1");
        /**
         * 更新文章点赞数量
         * */
        int i = aticleService.addGoodNum(Integer.parseInt(top_mid.getAticleId()));
        if (i == 1 && j == 1) {
            Map res = new HashMap();
            res.put("msg", "点赞成功");
            return respUtil.respSuccessHash(res);
        } else {
            String fReason = "失败!";
            return respUtil.respFailer(fReason);
        }
    }
}
